home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / sos3-2.lha / src / psm / mon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-23  |  6.4 KB  |  191 lines

  1. /* --------------------------------------------------------------------------
  2.  * Copyright 1992 by Forschungszentrum Informatik (FZI)
  3.  *
  4.  * You can use and distribute this software under the terms of the licence
  5.  * you should have received along with this program.
  6.  * If not or if you want additional information, write to
  7.  * Forschungszentrum Informatik, "STONE", Haid-und-Neu-Strasse 10-14,
  8.  * D-7500 Karlsruhe 1, Germany.
  9.  * --------------------------------------------------------------------------
  10.  */
  11. /* monitor of persistent storage manager: implementation */
  12. /* (empty, if MONITOR is not defined) */
  13.  
  14. #ifdef MONITOR
  15.  
  16. #include <sys/file.h>       // avoid warning for flock
  17. #include <X11/Xlib.h>
  18. #include <X11/Intrinsic.h>  // TRUE
  19.  
  20. #include "mon.h"
  21. #include "psm_err.h"        // err_raise, err_SYS
  22.  
  23. extern "C" {
  24. int usleep (unsigned);
  25. }
  26.  
  27. const uwait=15;
  28. const NOTICE=500000; // microsec
  29. const maxwin=10;
  30.  
  31. const size=9;    // size of each box in pixels
  32. const border=10; // size of border in pixels
  33.  
  34. const boxes=400; // number of black/white/grey boxes per window
  35. const dim=20;    // dim=sqrt(boxes), number of boxes in each dimension
  36. const height=dim*(size+1) + 1 + 2*border; // height of window contents in pixels
  37. const width=dim*(size+1) + 1 + 2*border;  // width of window contents in pixels
  38. const len = dim * (size + 1) + 1;
  39.  
  40. const char* ERR="from monitor of container manager";
  41.  
  42. enum Box {black,white,grey};
  43. enum Border {thick,thin,no};
  44.  
  45. struct wininfo {
  46.     int cid;     // cid=0 if unused
  47.     Window wid; };
  48. static wininfo wintable[maxwin];
  49.  
  50. static Display *disp;
  51. static GC gc;
  52.  
  53.  
  54. static void paintbox (Window wid, int nbr, Box b) {
  55.     if (nbr < boxes) {
  56.         const ycoord = border + 1 + (nbr / dim) * (size + 1);
  57.         const xcoord = border + 1 + (nbr % dim) * (size + 1);
  58.  
  59.         switch (b) {
  60.     case black: XSetForeground (disp, gc, BlackPixel (disp, 0));
  61.                     XSetFillStyle (disp, gc, FillSolid);
  62.             break;
  63.     case white: XSetForeground (disp, gc, WhitePixel (disp, 0));
  64.                     XSetFillStyle (disp, gc, FillSolid);
  65.             break;
  66.     case grey:  XSetFillStyle (disp, gc, FillTiled); }
  67.         XFillRectangle (disp, wid, gc, xcoord, ycoord, size, size);
  68.         usleep (uwait); } }
  69.  
  70.  
  71. static void paintborder (Window wid, Border b) {
  72.     switch (b) {
  73.     case thick: XSetForeground (disp, gc, BlackPixel (disp, 0));
  74.                 XSetBackground (disp, gc, WhitePixel (disp, 0));
  75.                 XSetLineAttributes (disp, gc, 5, LineSolid, CapRound, JoinRound);
  76.         break;
  77.     case thin:  paintborder (wid, no);
  78.                 XSetForeground (disp, gc, BlackPixel (disp, 0));
  79.                 XSetLineAttributes (disp, gc, 2, LineSolid, CapRound, JoinRound);
  80.         break;
  81.     case no:    XSetForeground (disp, gc, WhitePixel (disp, 0));
  82.                 XSetBackground (disp, gc, BlackPixel (disp, 0));
  83.                 XSetLineAttributes (disp, gc, 6, LineSolid, CapRound,JoinRound);}
  84.     XDrawRectangle (disp, wid, gc, border/2, border/2, border+len, border+len);
  85.     usleep(NOTICE); }
  86.  
  87.  
  88. static void writetitle (Window wid, char* title) {
  89.     if (title != 0) XStoreName (disp, wid, title);
  90.     else XStoreName (disp, wid, ""); }
  91.  
  92.  
  93. static void paintgrid (Window wid) {
  94.  
  95.     XSetForeground (disp, gc, BlackPixel (disp, 0));
  96.     XSetBackground (disp, gc, WhitePixel (disp, 0));
  97.     XSetFillStyle  (disp, gc, FillTiled);
  98.     XFillRectangle (disp, wid, gc, border, border, len, len);
  99.  
  100.     XSetFillStyle  (disp, gc, FillSolid);
  101.     XSetLineAttributes (disp, gc, 1, LineSolid, CapRound, JoinRound);
  102.     for (int yoff=0; yoff<len; yoff+=size+1)
  103.         XDrawLine (disp, wid, gc, border, border + yoff, 
  104.                   border + len - 1, border + yoff);
  105.  
  106.     for (int xoff=0; xoff<len; xoff+=size+1)
  107.         XDrawLine (disp, wid, gc, border + xoff, border, 
  108.               border + xoff, border + len - 1); }
  109.  
  110. static char greybg[] = {0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa};
  111.  
  112. void mon_initialize () {
  113.  
  114.     disp = XOpenDisplay(0);
  115.     if (disp == 0)
  116.        err_raise(err_SYS, err_MON_SERVER_CONNECTION_FAILED, NULL, FALSE);
  117.     XrmInitialize ();
  118.     gc = XCreateGC (disp, RootWindow (disp, 0), 0, 0);
  119.  
  120.     Pixmap tile = XCreatePixmapFromBitmapData (disp, RootWindow(disp,0), greybg,
  121.     8, 8, BlackPixel(disp,0), WhitePixel(disp,0), DefaultDepth(disp,0));
  122.     if (tile == 0)
  123.        err_raise(err_SYS, err_MON_PIXMAP_CREATION_FAILED, NULL, FALSE);
  124.     XSetTile (disp, gc, tile);
  125.  
  126.     XSynchronize(disp,TRUE);
  127.     for(int i=0; i<maxwin; i++) wintable[i].cid=0; }
  128.  
  129.  
  130. mon_Window mon_create (int cid, int last) {
  131.  
  132.     // check if window of cid already displayed and find free slot in wintable
  133.     int n=0; int freepos=maxwin;
  134.     while (n<maxwin && wintable[n].cid!=cid) {
  135.         if(wintable[n].cid==0) freepos=n;
  136.     n++; }
  137.   
  138.     if (n==maxwin) { // monitor of cid not yet displayed
  139.         if (freepos < maxwin) { // window table not exhausted,create new window
  140.             const windx = freepos * 135;
  141.             const windy = 20;
  142.             wininfo& win=wintable[freepos];
  143.             win.cid = cid;
  144.             win.wid = XCreateSimpleWindow(disp, RootWindow (disp, 0),
  145.                   windx, windy, width, height, 4,
  146.                   BlackPixel (disp, 0), WhitePixel (disp, 0));
  147.         XSetWindowAttributes attr;
  148.         attr.backing_store=Always;  // enables automatic repainting
  149.         // also makes waiting for first expose event obsolete !
  150.         XChangeWindowAttributes(disp, win.wid, CWBackingStore, &attr);
  151.             XMapRaised (disp, win.wid); // map window to make it visible
  152.         writetitle (win.wid, 0);
  153.         paintgrid (win.wid);
  154.             for (int nbr=0; nbr<last; nbr++) paintbox (win.wid, nbr, white);
  155.         usleep(NOTICE); } }
  156.     else  { // monitor already displayed
  157.         freepos=n; }
  158.  
  159.     return freepos; }
  160.  
  161.  
  162. void mon_destroy (mon_Window win) {
  163.     if (win < maxwin) {
  164.     XDestroyWindow (disp, wintable[win].wid);
  165.     wintable[win].cid = 0; } }
  166.  
  167.  
  168. void mon_open(mon_Window win, int rw, char* title) {
  169.     if (win < maxwin) {
  170.     if (rw) paintborder(wintable[win].wid, no);
  171.     else paintborder (wintable[win].wid, thin);
  172.     writetitle (wintable[win].wid, title); } }
  173.  
  174.  
  175. void mon_close(mon_Window win) {
  176.     if (win < maxwin) paintborder(wintable[win].wid, thick); }
  177.  
  178.  
  179. void mon_black (mon_Window win, int nbr) {
  180.     if (win < maxwin) paintbox (wintable[win].wid, nbr, black); }
  181.  
  182.  
  183. void mon_grey (mon_Window win, int nbr) {
  184.     if (win < maxwin) paintbox (wintable[win].wid, nbr, grey); }
  185.  
  186.  
  187. void mon_white (mon_Window win, int nbr) {
  188.     if (win < maxwin) paintbox (wintable[win].wid, nbr, white); }
  189.  
  190. #endif MONITOR
  191.